﻿namespace MeterReader.Common.Extensions
{
    public static class TaskExtensions
    {
        /// <summary>
        /// 用于在构造函数中可以调用异步方法
        /// </summary>
        /// <param name="task">异步方法</param>
        /// <param name="onCompleted">异步执行后回调</param>
        /// <param name="onError">异常回调</param>
        public static async void Await(this Task task, Action? onCompleted = null, Action<Exception>? onError = null)
        {
            try
            {
                await task;
                onCompleted?.Invoke();
            }
            catch (Exception ex)
            {
                onError?.Invoke(ex);
            }
        }
    }
}